<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/12 0012
 * Time: 下午 4:36
 */
namespace app\article\model;
use think\console\command\make\Model;
use think\Db;

class Compose extends Model{
    #全部文章列表
    public function all_list($data,$type){

        $keyword =isset($data['keyword'])?trim($data['keyword']):'';
        #条件
        $where ='a.del=1 and a.pid!=0';
        $where.=$keyword? ' and CONCAT(a.title) like "%'.$keyword.'%"':'';
        $where.=isset($data['id'])?' and a.pid='.$data['id']:'';
        $where.=isset($data['userid'])?' and a.userid='.$data['userid']:'';

        #分页
        $total = Db::name('compose')->where($where)->alias('a')
            ->join('member b','b.id=a.userid')
            ->count();#总数

        $pages = ceil($total/$data['limits']);#页数
        if ($data['curr']>$pages){
            $data['curr']=$pages;
        }
        $field = 'a.id,a.title,a.pid,a.userid,a.img,a.status,a.createtime,a.read,b.username';
        $list = Db::name('compose')->where($where)
            ->alias('a')
            ->join('member b','b.id=a.userid')
            ->field($field)
            ->page($data['curr'],$data['limits'])->order(['a.createtime'=>'desc'])->select();
        foreach ($list as $k =>$v){
            $comment = Db::name('comment')
                ->where(['compose_id'=>$v['id'],'del'=>1])->count();
            $reply = Db::name('reply')->where(['reply_id'=>$v['id'],'del'=>1])->count();
            $list[$k]['comment']=$comment+$reply;
            $list[$k]['like']=Db::name('likes')->where(['type_id'=>$v['id'],'type'=>1,'status'=>1])->count();
            $list[$k]['newtime']=dmy($list[$k]['createtime']);
        }
        $res = [
            'list'  =>$list,
            'total' =>$total,
            'curr'  =>$data['curr'],
            'limits'=>$data['limits'],
            'pages' =>$pages
        ];
        return $res;
    }
    #文章详情
    public function _list($data){
        $field='a.id,a.title,a.content,a.img as compose_img,a.createtime,a.read,a.userid,b.username,b.img';

        $res = Db::name('compose')
            ->alias('a')
            ->join('member b','b.id=a.userid')
            ->field($field)
            ->where(['a.id'=>$data['id']])->find();#找文章

        $res['login_user']=$data['userid'];

        $f ='m.username,m.img,c.content,c.createtime,c.id,from_userid';#对文章的全部评论
        $res['comment']=Db::name('comment')
            ->alias('c')
            ->join('member m','c.from_userid=m.id')
            ->where(['compose_id'=>$data['id'],'c.del'=>1,'m.del'=>1])
            ->field($f)
            ->order(['c.createtime'=>'desc'])
            ->select();

        foreach ($res['comment'] as $k =>$v){#上面所有评论的回复
            $fie = 'a.id,a.content,a.from_userid,a.createtime,b.username as from_user,b.img,c.username as to_user';
            $res['comment'][$k]['reply'] =Db::name('reply')
                ->alias('a')
                ->join('member b','b.id=a.from_userid')
                ->join('member c','c.id=a.to_userid')
                ->where(['a.comment_id'=>$v['id'],'a.del'=>1])
                ->order(['a.createtime'=>SORT_DESC])
                ->field($fie)
                ->select();
        }

        $res['count']=count($res['comment']);
        $res['like'] =Db::name('likes')->where(['type'=>1,'type_id'=>$data['id'],'status'=>1])->count();
        Db::name('compose')->where(['id'=>$data['id']])->update(['read'=>$res['read']+1]);

        return $res;
    }
    #按文章类别分类
    public function _type($data){
        $keyword =isset($data['keyword'])?trim($data['keyword']):'';
        #条件
        $where ='del=1 and status=1 and pid!=0';
        $where.=' and userid='.$data['userid'];
        #分类数量
        $total = Db::name('compose')
            ->where($where)
            ->field('type_id,count(type_id) as count')
            ->group('type_id')->select();
        foreach ($total as $key =>$value){
            $total[$key]['type_name']=Db('compose_type')
                ->where(['id'=>$value['type_id']])->value('type_name');
        }
        #分页
        $count = Db::name('compose')->where($where)->count();#总数
        $pages = ceil($count/$data['limits']);#页数
        if ($data['curr']>$pages){
            $data['curr']=$pages;
        }
        #分类内容
        $where.=$keyword? ' and type_id='.$keyword:'';
        $field = 'id,title,content,createtime,img,read';
        $list = Db::name('compose')->where($where)
            ->field($field)->page($data['curr'],$data['limits'])->order(['createtime'=>'desc'])->select();
        foreach ($list as $k =>$v){
            $list[$k]['time'] = dm($v['createtime']);#格式 20 sept
            $list[$k]['comment'] = Db::name('comment')
                ->where(['del' => 1, 'compose_id' => $v['id']])
                ->count();
            $list[$k]['likes'] = Db::name('likes')
                ->where(['status'=>1,'type'=>1,'type_id'=>$v['id']])
                ->count();
        }

        $res = [
            'list'=>$list,
            'total'=>$total,
            'count'=>$count,
            'curr'=>$data['curr'],
            'limits'=>$data['limits'],
            'pages'=>$pages
        ];
        return $res;
    }
    #按时间类别分类
    public function _time($data){
        $keyword =isset($data['keyword'])?trim($data['keyword']):'';
        #条件
        $where ='del=1 and status=1 and pid!=0';
        $where.=' and userid='.$data['userid'];
        #分类数量
        $total = Db::name('compose')
            ->where($where)
            ->field("date_format(createtime,'%Y年%m月') as time,count(date_format(createtime,'%Y%m')) as count")
            ->group("date_format(createtime,'%Y%m')")->select();

        #分页
        $count = Db::name('compose')->where($where)->count();#总数
        $pages = ceil($count/$data['limits']);#页数
        if ($data['curr']>$pages){
            $data['curr']=$pages;
        }

        #分类内容
        $where.=$keyword? " and date_format(createtime,'%Y%m')=".$keyword:'';
        $field = 'id,title,content,createtime,img,read';
        $list = Db::name('compose')->where($where)
            ->page($data['curr'],$data['limits'])->field($field)->order(['createtime'=>'desc'])->select();
        foreach ($list as $k =>$v){
            $list[$k]['time'] = dm($v['createtime']);#格式 20 sept
            $list[$k]['comment'] = Db::name('comment')
                ->where(['del' => 1, 'compose_id' => $v['id']])
                ->count();
            $list[$k]['likes'] = Db::name('likes')
                ->where(['status'=>1,'type'=>1,'type_id'=>$v['id']])
                ->count();
        }

        $res = [
            'list'  =>$list,
            'total' =>$total,
            'count' =>$count,
            'curr'  =>$data['curr'],
            'limits'=>$data['limits'],
            'pages' =>$pages
        ];
        return $res;
    }
    #添加文章
    public function _add($data){
        $add = Db::name('compose')->insertGetId($data);
        return $add;
    }
    #修改文章
    public function _update($data){
        $data_id=$data['id'];
        unset($data['id']);
        if (isset($data['img'])){

            $img = Db::name('compose')->where(['id'=>$data_id])->value('img');
            $url=$_SERVER["DOCUMENT_ROOT"]."/".$img;
            if (file_exists($url)){
                unlink($url);
            }
        }
        $update = Db::name('compose')->where(['id'=>['in',$data_id],'del'=>1])->update($data);
        return $update;
    }
}